#!/usr/bin/env python
#!coding=utf-8

from PySide.QtCore import QObject,Signal
from threading import Thread,currentThread
from Queue import Queue
from basegreecket import *
from time import sleep

from wx import Object,PyCommandEvent,NewEventType,PyEventBinder

class GreeEvent(PyCommandEvent):
    def __init__(self,evtType,id,eventid,data,peer):
        '''
        eventid,發生事件的id
        data,事件相應數據
        peer,發生事件的鏈接
        '''
        PyCommandEvent.__init__(self,evtType,id)
        self._EventId=eventid
        self._Data=data
        self._Peer=peer
        self._EventObject=peer
        self._AsList=[eventid,data,peer]

    def __getitem__(self,index):
        if index < len(self._AsList):
            return self._AsList[index]
        return None

    @property
    def AsList(self):
        return self._AsList
    @property
    def Peer(self):
        return self._Peer
    @property
    def Data(self):
        return self._Data
    @property
    def EventObject(self):
        return self._EventObject

EVT_GREE_READY=PyEventBinder(NewEventType(),1)
EVT_GREE_LISTEN=PyEventBinder(NewEventType(),1)
EVT_GREE_CONNECTING=PyEventBinder(NewEventType(),1)
EVT_GREE_CONNECTIONLOST=PyEventBinder(NewEventType(),1)
EVT_GREE_CONNECTIONMADE=PyEventBinder(NewEventType(),1)
EVT_GREE_DATARECEIVED=PyEventBinder(NewEventType(),1)#完整數據組接收完成
EVT_GREE_PACKRECEIVED=PyEventBinder(NewEventType(),1)#數據包接收完成
EVT_GREE_DATATOSEND=PyEventBinder(NewEventType(),1)#數據準備發送
EVT_GREE_DATASEND=PyEventBinder(NewEventType(),1)#完整數據組發送完成
EVT_GREE_PACKSEND=PyEventBinder(NewEventType(),1)#數據包發送完成
EVT_GREE_ERROR=PyEventBinder(NewEventType(),1)
EVT_GREE_ERROR_RECV=PyEventBinder(NewEventType(),1)
EVT_GREE_ERROR_SEND=PyEventBinder(NewEventType(),1)
EVT_GREE_ERROR_LISTEN=PyEventBinder(NewEventType(),1)
EVT_GREE_ERROR_CONNECT=PyEventBinder(NewEventType(),1)
EVT_GREE_SHUTDOWN=PyEventBinder(NewEventType(),1)

class Factory(Object):

    def __init__(self,parent=None,address=(),multipeer=True,limit=5):
        '''
        _EvtBinds['__all__']為響應所有對象事件的字典
        '''
        #Object.__init__(self)
        if multipeer:
            self._Greecket=MultiGreecket(self,address)
        else:
            self._Greecket=BaseGreecket(self,address)
        self.parent=parent
        self._EvtBinds=dict([(GREE_READY,EVT_GREE_READY),
                  (GREE_LISTEN,EVT_GREE_LISTEN),
                  (GREE_CONNECTING,EVT_GREE_CONNECTING),
                  (GREE_CONNECTIONLOST,EVT_GREE_CONNECTIONLOST),
                  (GREE_CONNECTIONMADE,EVT_GREE_CONNECTIONMADE),
                  (GREE_DATARECEIVED,EVT_GREE_DATARECEIVED),
                  (GREE_PACKRECEIVED,EVT_GREE_PACKRECEIVED),
                  (GREE_DATATOSEND,EVT_GREE_DATATOSEND),
                  (GREE_DATASEND,EVT_GREE_DATASEND),
                  (GREE_PACKSEND,EVT_GREE_PACKSEND),
                  (GREE_ERROR,EVT_GREE_ERROR),
                  (GREE_ERROR_RECV,EVT_GREE_ERROR_RECV),
                  (GREE_ERROR_SEND,EVT_GREE_ERROR_SEND),
                  (GREE_ERROR_LISTEN,EVT_GREE_ERROR_LISTEN),
                  (GREE_ERROR_CONNECT,EVT_GREE_ERROR_CONNECT),
                  (GREE_SHUTDOWN,EVT_GREE_SHUTDOWN)])
        
    def getGreecket(self):
        return self._Greecket

    def senfTo(self,machine,data):
        '''
        通過machine相應的鏈接發送數據data
        '''
        
        pass

    def pushEvent(self,event):
        #print 'wxfactory.pushEvent 101:',event
        et=self._EvtBinds[event.EventId]
        evt=GreeEvent(et.evtType[0],self.parent.GetId(),event.EventId,event.Data,event.Peer)
        #print evt
        self.parent.GetEventHandler().ProcessEvent(evt)
        
    def listen(self,address=None,limit=5):
        self._Greecket.listen(address,limit)
        
    def connect(self,address=None):
        self._Greecket.connect(address)

    def stop(self):
        self._Greecket.stop()
        self._EvtBinds.clear()
        
    def start(self):
        self._Greecket.start()

    def isRunning(self):
        return self._Greecket.isAlive()

    def bind(self,event,callback,binder=None):
        '''
        將事件處理函數callback綁定事件event
        綁定成功返回True,否則返回False
        binder為None時,callback響應所有對象的event事件
        '''
        self.parent.Bind(event,callback)
        #self._EvtBinds[event].connect(callback)

    def unBind(self,event,callback=None,binder=None):
        '''
        取消函數callback在事件event上的綁定，沒有指定callback時，取消event事件的所有綁定函數,沒有指定binder時取消所有綁定對象的事件
        '''
        if event and callback:
            self._EvtBinds[event].disconnect(callback)
        elif event:
            self._EvtBinds[event].disconnect()

    def clearBind(self):
        '''
        取消所有綁定的事件
        '''
        for s in self._EvtBinds:
            s.disconnect()

    def register(self,event):
        '''
        註冊未定義的事件,
        只有事件註冊後才能使用bind進行綁定
        如果指定的event已存在,返回False,否則返回True
        '''
        return None

    def removeBind(self,binder):
        '''
        刪除一個對象的所有綁定事件,並將綁定對象從
        '''
        return None

    def _eventCheck(self,event):
        return None

